<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) wangyt <wangtom365@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace App\Http\Controllers;

use App\Models\ModuleModel;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;

class BaseController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    // ------------------------------------------------------------------------

    /**
     * @param string $file
     *
     * @throws \Exception
     *
     * @return mixed
     */
    public function getYamlContent($file = '')
    {
        try {
            if (is_file($file)) {
                return Yaml::parse(file_get_contents($file));
            }
            throw new \Exception('File Not Found: '.$file);
        } catch (ParseException $e) {
            throw new \Exception(sprintf('Unable to parse the YAML string: %s', $e->getMessage()));
        }
    }

    // ------------------------------------------------------------------------

    /**
     * @param type $arrData
     *
     * @return bool
     */
    public static function renderApi($arrData)
    {
        if (empty($arrData)) {
            return false;
        }
        echo json_encode($arrData);
        exit;
    }

    // ------------------------------------------------------------------------

    /**
     * @param string $msg
     * @param array  $data
     */
    public static function renderApiSuccess($msg = 'OK', $data = [])
    {
        $result = [
            'code' => 1,
            'msg' => $msg,
            'data' => $data,
            'time' => date('Y-m-d H:i:s'),
        ];
        echo json_encode($result);
        exit;
    }

    // ------------------------------------------------------------------------

    /**
     * @param string $msg
     * @param int    $code
     * @param array  $data
     */
    public static function renderApiError($msg = 'ERROR', $code = 0, $data = [])
    {
        $result = [
            'code' => 1001,
            'msg' => 'ERROR',
            'date' => date('Y-m-d H:i:s'),
        ];
        if (isset($msg)) {
            $result['msg'] = $msg;
        }
        if ($code) {
            $result['code'] = (int) $code;
        }
        if ($data) {
            $result['data'] = $data;
        }
        echo json_encode($result);
        exit;
    }

    // ------------------------------------------------------------------------
    protected function getModuleList($parentId = 0)
    {
        $conditions = collect([
            'where' => [
                'module_status' => 1,
            ],
        ]);
        $moduleResult = (new ModuleModel())->getListData($conditions);

        return collect($moduleResult)->mapWithKeys(function ($item) {
            return [
                $item['module_id'] => $item,
            ];
        })->toArray();
    }

    // ------------------------------------------------------------------------
}
